package com.kuangqiyuan.yatzygame.rule;

import com.kuangqiyuan.yatzygame.Play;

import java.util.Arrays;

/**
 * 处理两种规则
 * - 小顺子
 * - 大顺子
 */
public class StraightRuleHandler extends AbstractRuleHandler {
  public StraightRuleHandler() {
    super();
  }

  @Override
  public int Count() {
    int len = this.play.getLength();
    int[] records = new int[len];

    for (int i = 0; i < len; i++) {
      records[i] = this.play.getRecord(i);
    }

    Arrays.sort(records);

    int current = 0;
    int countSum = 0;
    int score = 0;

    for (int i = 0; i < records.length; i++) {
      if (i == 0) {
        current = records[i];
        continue;
      }

      if (current + 1 != records[i]) {  // 不是顺子
        return 0;
      }

      countSum++;
      current = records[i];
    }

    for (int i : records) {
      score += i;
    }

//    System.out.println(String.format("%s counts: %d", this.getClass(), score));

    return score;
  }
}
